package com.ruoyi.file.controller;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.file.service.AliOssSysFileServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.file.FileUtils;
import com.ruoyi.system.api.domain.SysFile;

/**
 * 文件请求处理
 * 
 * @author ruoyi
 */
@RestController
public class SysFileController
{
    private static final Logger log = LoggerFactory.getLogger(SysFileController.class);


    @Autowired
    private AliOssSysFileServiceImpl aliOssSysFileService;


    @GetMapping("/delFiles/{filesName}")
    public AjaxResult delFiles(@PathVariable("filesName") String filesName){
        System.out.println("filesName:"+filesName);
        aliOssSysFileService.deleteOssFile(filesName);
        return null;
    }

    /**
     * 文件上传请求
     */
    @PostMapping("upload")
    public R<SysFile> upload(MultipartFile file)
    {
        try {
            String fileName = aliOssSysFileService.uploadFile(file);
            if (StringUtils.isEmpty(fileName)) {
                return R.fail("出现异常");
            }
            SysFile sysFile = new SysFile();
            sysFile.setName(FileUtils.getName(fileName));
            sysFile.setUrl(fileName);
            System.out.println(fileName);
            return R.ok(sysFile);
        } catch (Exception e) {
            return R.fail("文件上传失败:" + e.getMessage());
        }
    }
}